<?php
namespace app\app\controller\v1;
use app\app\Appbase;

//
//评论
//
class Comment extends Appbase 
{

	/**
	* 新增评论
	* /app/v1/comment/add
	* 是否需要登录 
	* @is_need_login true
	*@method post
	*/
	public function add($order_id,$comment){
		if( empty($order_id) ||  empty($comment)){
			return $this->ReError("参数错误！");	
		}
		
		$param=  request()->post();
		$uid = $param['uid']  = request() ->userId;
		
		$order = model('Order')->where('uid',$uid)->where('id',$order_id)->find();
		if(empty($order)){
			return $this->ReError("订单不存在！");	
		}
		if( $order['status'] != '2' ){
			return $this->ReError("订单状态不允许评论！");	
		}
		
		if(model('GoodsReply')->where('uid',$uid)->where('order_id',$order_id)->find()){
			return $this->ReError("已评论过该订单！");	
		}
		
		$goodsIds = model('OrderCart')->getGoodsInfo($order_id,"goods_id");
		$data = [];
		foreach ($goodsIds as $key => $value) {
			$param['goods_id'] = $value;
			$data[] = $param;
		}
		
		\Db::startTrans();
		try {
			$order->status = 3;
			$order->is_evaluate = 1;
			$res = $order->save();
			$res = $res && model('GoodsReply')->saveAll($data);
			if(!$res){
				return $this->ReError("异常错误！");	
				\Db::rollback();
			}
			\Db::commit();
			return $this->ReSucess();	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 我的评论列表
	* /app/v1/comment/getList
	* 是否需要登录 
	* @is_need_login true
	*/
	public function getList(){
		$uid = $data['uid']  = request() ->userId;
		try {
			
			$result = model("GoodsReply")
				->where('uid',$uid)
				->order("add_time desc")
				->group('order_id')
				->select();
				
			if( empty($result)  ) return $this->ReError("请求错误");	
			
			return $this->ReSucess($result);	
			
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 删除评论
	* /app/v1/comment/getList
	* 是否需要登录 
	* @is_need_login true
	*/
	public function del($id){
		
		$uid = $data['uid']  = request() ->userId;
		
		try {
			
			$result = model("GoodsReply")
				->where('uid',$uid)
				->where('id|order_id|goods_id',$id)
				->delete();
				
			return $this->ReSucess($result);	
			
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
}



